package com.cloudsigns.factorymanager2.project.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.TeamReceiveOrBackEngineeringInstallOrderPresenter;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.iview.ISurveyOrderDetailForTeamView;
import com.cloudsigns.factorymanager2.project.iview.ISurveyOrderReceiveOrBackForTeamView;
import com.cloudsigns.factorymanager2.project.presenter.SurveyOrderDetailForTeamPresenter;
import com.cloudsigns.factorymanager2.project.presenter.SurveyOrderReceiveOrBackForTeamPresenter;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**fragement_survey_workorder_info_for_team
 * Created by tony on 2018/8/5.
 */

public class SurveyWordOrderInfoForTeamFragment  extends Fragment implements ISurveyOrderReceiveOrBackForTeamView, View.OnClickListener{
    private final String TAG = SurveyWordOrderInfoForTeamFragment.class.getSimpleName();
    private TextView tv_project_name, tv_engineering_name;
    private TextView edt_engineering_address, edtcompletion_date, edt_contact, edt_contact_phone, edt_dispath_order_person_colon,edt_dispath_order_time_colon;
     private View ll_receive_back_btn;
    private View rl_project;
   private TextView btn_receive_order, btn_back_order;
    private boolean isClick = true;
    private CommonEnum.STATUS status;
     private LinearLayout ll_engineering_info_content;
    private ImageView img_project_select;
    private SurVeyOrderBean surVeyOrderBean;
    private SurveyOrderReceiveOrBackForTeamPresenter surveyOrderReceiveOrBackForTeamPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surveyOrderReceiveOrBackForTeamPresenter = new SurveyOrderReceiveOrBackForTeamPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragement_survey_workorder_info_for_team, null);
        tv_project_name = view.findViewById(R.id.tv_project_name);
        tv_engineering_name = view.findViewById(R.id.tv_engineering_name);
        edt_engineering_address = view.findViewById(R.id.edt_engineering_address);

//      edt_engineering_address = findViewById(R.id.edt_engineering_address);
        edtcompletion_date = view.findViewById(R.id.edtcompletion_date);
        edt_contact = view.findViewById(R.id.edt_contact);
        edt_contact_phone = view.findViewById(R.id.edt_contact_phone);
        edt_dispath_order_person_colon = view.findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon = view.findViewById(R.id.edt_dispath_order_time_colon);
        ll_engineering_info_content = view.findViewById(R.id.ll_engineering_info_content);
        img_project_select = view.findViewById(R.id.img_project_select);
        rl_project = view.findViewById(R.id.rl_project);
        ll_receive_back_btn = view.findViewById(R.id.ll_receive_back_btn);

        btn_receive_order = view.findViewById(R.id.btn_receive_order);
        btn_back_order = view.findViewById(R.id.btn_back_order);


        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        rl_project.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);

        return view;
    }

    public void initData(SurVeyOrderBean surVeyOrderBean) {
        if (null != surVeyOrderBean) {
            this.surVeyOrderBean = surVeyOrderBean;

            tv_project_name.setText(surVeyOrderBean.getProjectName());
            tv_engineering_name.setText("/" + surVeyOrderBean.getEngineeringName());

            edt_engineering_address.setText(surVeyOrderBean.getEngineeringAddress());
            edtcompletion_date.setText(DateUtil.getYyyy_Mm_Dd(surVeyOrderBean.getCompleteDate()));
            edt_contact.setText(surVeyOrderBean.getContacts());
            edt_contact_phone.setText(surVeyOrderBean.getContactsPhone());
            edt_dispath_order_person_colon.setText(surVeyOrderBean.getReceiveAccountName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(surVeyOrderBean.getSendOrderTime()));


            CommonEnum.EngineeringOrderStatus engineeringOrderStatus = surVeyOrderBean.getStatus();
//            if (null != engineeringOrderStatus) {
//                if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.CREATED) {
////                    edt_order_statuse_colon.setText("[创建/未编辑]");
//                    isClick = true;
//                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.EDITED) {
////                    edt_order_statuse_colon.setText("[已编辑/未派发]");
//                    isClick = true;
//                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.SEND) {
////                    edt_order_statuse_colon.setText("[派发中]");
//                    isClick = false;
//                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.RECEIVED) {
////                    edt_order_statuse_colon.setText("[已经接收]");
//                    isClick = false;
//                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.RETURNED) {
////                    edt_order_statuse_colon.setText("[退回]");
//                    isClick = false;
//                } else if (engineeringOrderStatus == CommonEnum.EngineeringOrderStatus.COMPLETED) {
////                    edt_order_statuse_colon.setText("[竣工");
//                    isClick = false;
//                }
//            }


            CommonEnum.AssignStatus assignStatus = surVeyOrderBean.getAssignStatus();
            if (null != assignStatus) {
                if (assignStatus == CommonEnum.AssignStatus.CREATED) {
                    ll_receive_back_btn.setVisibility(View.VISIBLE);
//                    edt_tv_dispatch_status_colon.setText("[创建/未编辑]");
                } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {
                    ll_receive_back_btn.setVisibility(View.VISIBLE);
//                    edt_tv_dispatch_status_colon.setText("[已编辑/未派发]");
                } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                    ll_receive_back_btn.setVisibility(View.VISIBLE);
//                    edt_tv_dispatch_status_colon.setText("[派发中]");
                } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
                    ll_receive_back_btn.setVisibility(View.GONE);
//                    edt_tv_dispatch_status_colon.setText("[已经接收]");
                } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
                    ll_receive_back_btn.setVisibility(View.GONE);
//                    edt_tv_dispatch_status_colon.setText("[退回]");
                } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
                    ll_receive_back_btn.setVisibility(View.GONE);
//                    edt_tv_dispatch_status_colon.setText("[竣工");
                }
            }
//            if (engineeringInstallationOrderBean.isHasPauseOrder()) {
//                rl_wether_pause_colon.setVisibility(View.VISIBLE);
//                edt_wether_pause_colon.setText("{[暂停]");
//            } else
//                rl_wether_pause_colon.setVisibility(View.GONE);


        }

        btnClickable();
    }

    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void btnClickable() {
        if (isClick) {

            btn_receive_order.setBackgroundResource(R.color.rgb_00B050);

            btn_back_order.setBackgroundResource(R.color.rgb_C00000);
        } else {

            btn_receive_order.setBackgroundResource(R.color.rgb_D9D9D9);

            btn_back_order.setBackgroundResource(R.color.rgb_D9D9D9);
        }
    }


    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        btnClickable();
        surveyOrderReceiveOrBackForTeamPresenter.surveyOrderReceiveOrBackForTeam();
    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        btnClickable();
        surveyOrderReceiveOrBackForTeamPresenter.surveyOrderReceiveOrBackForTeam();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.rl_project:
                showEngineeringInfoDialog();
                break;
            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
            case R.id.btn_back_order:
                if (isClick)
                    backOrder();
                break;


        }
    }

    @Override
    public String getOrderId() {
        if (null != surVeyOrderBean)
            return surVeyOrderBean.getId();
        return "";
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void surveyOrderReceiveOrBackForTeamSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
                Toast.makeText(getActivity(), "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(getActivity(), "退回成功！", Toast.LENGTH_SHORT).show();
        }
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        isClick = false;
        btnClickable();
    }

    @Override
    public void surveyOrderReceiveOrBackForTeamFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
        isClick = true;
        btnClickable();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
